<?php

class IndexController extends CloudControllerBase
{
    //主页
    public function indexAction()
    {
        $user = $this->getMonitorLoginUser(true);
        if (empty($user)) {
            $this->response->redirect("../user/login");
        }
        switch ($user->userRoleId) {
            case MonitorLoginUser::USER_ROLE_SYSTEM_ADMIN: {
                $menuInfo = $this->getAuthMenu();
            };
                break;
            case MonitorLoginUser::USER_ROLE_SUBSYSTEM_ADMIN: {

                $menuInfo = $this->defaultAuthMenu['subadmin'];
            };
                break;
            case MonitorLoginUser::USER_ROLE_NORMAL: {

                $menuInfo = $this->defaultAuthMenu['normal'];
            };
                break;
        }
        $this->view->setVars(array("user" => $user,
                "menu_data" => json_encode($menuInfo),
                "menuInfo" => $menuInfo,
                "index_url" => "/index/content",
                "token" => $user->token,
                "web_socket" => $this->host ['webSocket']
            )

        );
    }

    private function getAuthMenu()
    {
        $menuDict = $this->defaultAuthMenu['admin'];
        $authMenu = $this->getMonitorLoginUser()->userAuthMenu;
        if ($authMenu) {
            $newDict = Array();
            foreach ($menuDict as $itemArray) {
                $newItemArray = $this->copyMenuItem($itemArray);
                foreach ($itemArray["children"] as $item) {
                    $result = strpos($authMenu, $item["menuUrl"]);

                    if ($result || (is_numeric($result) && result >= 0)) {
                        array_push($newItemArray["children"], $this->copyMenuItem($item));
                    }
                }
                array_push($newDict, $newItemArray);
            }
            return $newDict;
        } else {
            return $menuDict;
        }
    }

    private function copyMenuItem($source)
    {
        return array(
            "name" => $source["name"],
            "menuUrl" => $source["menuUrl"],
            "iconUrl" => $source["iconUrl"],
            "bigIconUrl" => $source["bigIconUrl"],
            "background" => $source["background"],
            "children" => array()
        );
    }

    //获取用户菜单

    public function contentAction()
    {
        $user=$this->getMonitorLoginUser();

        $this->view->setVars(array(
            "user"=>$user
        ));
    }


    public function detailEditAction()
    {

    }

    public function errorAction()
    {

    }



}

